package ventureco.task;

import java.util.ArrayList;

import ventureco.map.MapImpl;

public class TaskCreator {
	
	public ArrayList<Task> createTasks(int x,int y,int n)
	{
		ArrayList<Task> tasklist = new ArrayList<Task>();
		tasklist = divide(x,y,n);
		//tasklist = divideWithKMeans(x,y,n);
		for(int i = 0;i<tasklist.size();i++)
		{
			//System.out.println("Task" + i +"'s Coordinates : ");
//			System.out.println("Point 1 : " + tasklist.get(i).x1.x + " " + tasklist.get(i).x1.y);
//			System.out.println("Point 2 : " + tasklist.get(i).x2.x + " " + tasklist.get(i).x2.y);
//			System.out.println("Point 3 : " + tasklist.get(i).y1.x + " " + tasklist.get(i).y1.y);
//			System.out.println("Point 4 : " + tasklist.get(i).y2.x + " " + tasklist.get(i).y2.y);
//			double area = (tasklist.get(i).x1.getX() - tasklist.get(i).x2.getX()) * (tasklist.get(i).x1.getY() - tasklist.get(i).y1.getY());
//			System.out.println("Area of task" + i + ": " + area);
		}
		return tasklist;
	}
	
	public static int getAgentCount()
	{
		return 10;
	}
	
	public static float getTaskAreaSize()
	{
		return 10000;
	}
	
	public static float getScala()
	{
		return 100;
	}
	
	public static ArrayList<Task> divide(int x,int y,int n)
	{
		ArrayList<Task> taskList = new ArrayList<Task>();
		
		Task t;
		char longSide;
		if(x > y)
			longSide = 'x';
		else 
			longSide = 'y';
		
		if(longSide == 'x')
		{
			for(int i = 0;i<n;i++)
			{
				t = new Task();
				t.bottomLeft.x = i * (x / n);
				t.bottomLeft.y = 0;
				t.bottomRight.x = (i+1) * (x/n);
				t.bottomRight.y = 0;
				t.topLeft.x = i * (x / n);
				t.topLeft.y = y;
				t.topRight.x = (i+1) * (x/n);
				t.topRight.y = y;
				t.setStartingLocation();
				taskList.add(t);
			}
		}
		else if (longSide == 'y')
		{
			for(int i = 0;i<n;i++)
			{
				t = new Task();
				t.bottomLeft.x = 0;
				t.bottomRight.x = x;
				t.topLeft.x = 0;
				t.topRight.x = x;
				t.bottomLeft.y = i * (y / n);
				t.bottomRight.y = i * (y / n);
				t.topLeft.y = (i + 1) * (y / n);
				t.topRight.y = (i + 1) * (y / n);
				
				taskList.add(t);
			}
			
			
		}
			
			
			
		
		
		
		return taskList;
	}
	
	public static ArrayList<Task> divideWithKMeans(int a,int b,int n)
	{
		Task t;
		ArrayList<Task> taskList = new ArrayList<Task>();
		 Kmeans kmeans = new Kmeans(MapImpl.AppFrame.elevationOfSelectedMap, n);
	        //kmeans.setEpsilon(0.000001);
	    kmeans.calculateClusters();
		ArrayList[] clusters = kmeans.getClusters();
		
		int minx = 1000,maxx = 0,miny = 1000,maxy = 0; 
		 for(ArrayList cluster: clusters)
	        {
	            System.out.println("CLUSTER");
	            for(Object dat:cluster)
	            {
	                for(int i=0;i<2;i++)
	                {
	                	if(i == 0)
	                	{
	                		int x = (int)(((double[])dat)[i]);
	                		if(x >= maxx)
	                			maxx = x;
	                		else if(x <= minx)
	                			minx = x;
	                	}
	                	else if(i == 1)
	                	{
	                		int y = (int)(((double[])dat)[i]);
	                		if(y >= maxy)
	                			maxy = y;
	                		else if(y <= miny)
	                			miny = y;
	                	}
	                }
	            }
	            t = new Task();
				t.bottomLeft.x = minx;
				t.bottomLeft.y = miny;
				t.bottomRight.x = maxx;
				t.bottomRight.y = miny;
				t.topLeft.x = minx;
				t.topLeft.y = maxy;
				t.topRight.x = maxx;
				t.topRight.y = maxy;
				t.setStartingLocation();
				taskList.add(t);
				minx = 1000;maxx = 0;miny = 1000;maxy = 0;
	        }
		
		return taskList;
	}
	
	
	
	public static ArrayList<Task> divide2(int x,int y,int n)
	{
		ArrayList<Task> taskList = new ArrayList<Task>();
//		Point2D point ;
//		ArrayList<Point2D> coords = new ArrayList<Point2D>();
		int agentCount = getAgentCount();
		char longSide;
		if(x > y)
			longSide = 'x';
		else 
			longSide = 'y';
		
		int Max = Math.max(x,y);
		int Min = Math.min(x,y);
		float q = Max / Min;
		
		float region = agentCount / q ;
		
		Task t = null;
		if( q > (agentCount / 2))
		{
			if(longSide == 'x')
			{
				for(int i = 0 ;i<agentCount;i++)
				{
					t = new Task();
					t.bottomLeft.x = i * (x / n);
					t.bottomLeft.y = 0;
					t.bottomRight.x = (i+1) * (x/n);
					t.bottomRight.y = 0;
					t.topLeft.x = i * (x / n);
					t.topLeft.y = y;
					t.topRight.x = (i+1) * (x/n);
					t.topRight.y = y;
					t.setStartingLocation();
					taskList.add(t);
				}
			}
			else if(longSide == 'y')
			{
				for(int i = 0 ;i<agentCount;i++)
				{
					t = new Task();
					t.bottomLeft.x = 0;
					t.bottomRight.x = x;
					t.topLeft.x = 0;
					t.topRight.x = x;
					t.bottomLeft.y = i * (y / n);
					t.bottomRight.y = i * (y / n);
					t.topLeft.y = (i + 1) * (y / n);
					t.topRight.y = (i + 1) * (y / n);
					taskList.add(t);
				}
			}
			
		}
		else
		{
			int R = (int) region;
			//System.out.println("not implemented yet");
			
			
			
			
		}
		
		
		
		
		return taskList;
	}

}
